package com.xzz.finger;

/**
 * @author: hhz
 * @create: 2022-02-15 13:53
 * 剑指 Offer 04. 二维数组中的查找
 **/
public class FindNumberIn2DArray {
    public static void main(String[] args) {
        System.out.println(findNumberIn2DArray(new int[][]{{1, 4, 7, 11, 15}, {2, 5, 8, 12, 19}, {3, 6, 9, 16, 22}, {10, 13, 14, 17, 24}, {18, 21, 23, 26, 30}}, 5));
    }

    public static boolean findNumberIn2DArray(int[][] matrix, int target) {
        //从下往上(左下角元素)
        int j = 0;
        int i = matrix.length - 1;
        while (i >= 0 && j < matrix[0].length) {
            int num = matrix[i][j];
            if (num > target) {
                i--;
            } else if (num<target){
                j++;
            }else {
                return true;
            }
        }
        return false;
    }
}
